<template>

    <div class="navbar">
        <div class="nav-item-left">
            <div class="nav-item" :class="{ active: $route.path === '/gindex' }"
                @click.prevent="$router.push('/gindex')">
                <span>首页</span>
            </div>
            <div class="nav-item" :class="{ active: $route.path === '/coursedetail' }"
                @click.prevent="$router.push('/coursedetail')">
                <span>课程详细</span>
            </div>
            <div class="nav-item" :class="{ active: $route.path === '/login' }" @click.prevent="$router.push('/login')">
                <span>登录</span>
            </div>
            <div class="nav-item" :class="{ active: $route.path === '/register' }"
                @click.prevent="$router.push('/register')">
                <span>注册</span>
            </div>
            <div class="nav-item" :class="{ active: $route.path === '/Oindex' }"
                @click.prevent="$router.push('/Oindex')">
                <span>个人中心</span>
            </div>
            <div class="nav-item" :class="{ active: $route.path === '/VideoPlayer' }"
                @click.prevent="$router.push('/VideoPlayer')">
                <span>小组分工</span>
            </div>
        </div>
        <div
            style="display: flex; justify-content: center; padding: 10px; background-color: #f2f2f2; border-radius: 5px;">
            <button> <a href="https://www.bilibili.com/video/BV1Zw4m1S7Ev/" target="_blank"
                    style="color: lightpink; text-decoration: none; font-weight: bold;">B站视频地址</a> </button>
            <button> <a href="https://gitee.com/gao-weikeng/final" target="_blank"
                    style="color: lightblue; text-decoration: none; font-weight: bold;">代码仓库地址</a> </button>
        </div>

        <div class="navbar-icon" @mouseenter="showDropdown = true" @mouseleave="showDropdown = false">
            <img src="https://codelover.club/files/stutasks/userID_874/currNo_47/FvqQzLQzCCC4GY4B03QSAid3OAgf.jpg"
                alt="Navbar Icon" width="60" height="60" style="margin-right: 70px">
            <div class="dropdown" v-if="showDropdown">
                <div class="dropdown-item">
                    <div class="dropdown-content">
                        <div class="dropdown-info">
                            <img src="https://codelover.club/files/stutasks/userID_874/currNo_47/FvqQzLQzCCC4GY4B03QSAid3OAgf.jpg"
                                alt="Navbar Icon" width="120" height="120">
                            <div>
                                <div class="dropdown-name">
                                    <h3>{{ useUser.getUname() }}</h3>
                                    <router-link to="/oindex" class="navbar-link">个人主页</router-link>
                                </div>
                                <div class="dropdown-role">
                                    大学生
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="dropdown-links">
                        <router-link to="/Mycourse" class="navbar-link">
                            <img src="https://codelover.club/files/stutasks/userID_874/currNo_47/FvqQzLQzCCC4GY4B03QSAid3OAgf.jpg"
                                alt="Countdown Icon" width="20" height="20">
                            我的课程
                        </router-link>
                        <router-link to="/LearningSquare" class="navbar-link">
                            <img src="https://codelover.club/files/stutasks/userID_874/currNo_47/FvqQzLQzCCC4GY4B03QSAid3OAgf.jpg"
                                alt="Countdown Icon" width="20" height="20">
                            学习广场
                        </router-link>
                    </div>
                    <div class="dropdown-links">
                        <router-link to="/Manage" class="navbar-link">
                            <img src="https://codelover.club/files/stutasks/userID_874/currNo_47/FvqQzLQzCCC4GY4B03QSAid3OAgf.jpg"
                                alt="Countdown Icon" width="20" height="20">
                            管理中心
                        </router-link>
                    </div>
                </div>
                <div class="dropdown-item">
                    <h2 style="font-size: 24px; color:aqua;text-align: center;font-weight: bold;">
                        在学课程
                    </h2>
                    <div class="dropdown-title" v-for="(course, index) in courseData" :key="index">
                        <h4>{{ course.name }}</h4>
                        <img :src="course.iconPath" alt="Course Icon" width="20" height="20" />
                    </div>
                    <div class="logout">
                        <el-button @click="logout()" type="danger">退出登录</el-button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { RouterLink, RouterView } from 'vue-router';
import { useUserStore } from '../stores/userStore';
import useDataStore from "../stores/Data";

export default {
    inject: ['homeData'],

    data() {
        return {
            showDropdown: false,
            courseData: this.homeData,
        }
    },
    mounted() {
        const useData = useDataStore();
        useData.getHomeData().then((data) => {
            this.name = data.name;
        });
    },

    setup() {
        const useUser = useUserStore();

        const logout = async () => {
            try {
                await useUser.logout(); // 假设 logout 是异步的
                // 如果需要，可以在这里添加重定向到登录页面的代码
                router.push('/login');
            } catch (error) {
                // console.error('退出登录失败', error);
            }
        };

        return {
            logout,
            useUser,
        };
    },
    beforeRouteEnter(to, from, next) {
        // 如果是 login 或 register 或 exgindex 路由，则不显示导航栏
        if (to.path === '/login' || to.path === '/register' || to.path === '/exgindex') {
            next(vm => {
                vm.$parent.showNavbar = false; // 假设 App.vue 中有一个 showNavbar 数据属性
            });
        } else {
            next(vm => {
                vm.$parent.showNavbar = true; // 其他路由则显示导航栏
            });
        }
    },

    // 路由更新时
    beforeRouteUpdate(to, from, next) {
        if (to.path === '/login' || to.path === '/register' || to.path === '/exgindex') {
            this.$parent.showNavbar = false; // 假设 App.vue 中有一个 showNavbar 数据属性
        } else {
            this.$parent.showNavbar = true; // 其他路由则显示导航栏
        }
        next();
    }
}
</script>

<style scoped>
.navbar {
    left: 0;
    position: fixed;
    top: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
    background-color: #f1f1f1;
    z-index: 999;
}

.navbar-icon {
    position: relative;
    cursor: pointer;
}

.dropdown {
    position: absolute;
    top: 60px;
    right: 0;
    width: 270px;
    background-color: #f0f8ff;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    padding: 20px;
    border-radius: 8px;
    z-index: 1000;
}

.dropdown-item {
    padding: 10px;
    border-radius: 5px;
    margin-bottom: 20px;
    border-bottom: 1px solid #e0e0e0;
    padding-bottom: 20px;
}

.dropdown-title {
    display: flex;
    font-display: row;
    align-items: center;
    padding: 5px;
}

.dropdown-content {
    display: flex;
    align-items: center;
}

.dropdown-info {
    display: flex;
}

.dropdown-info img {
    margin-right: 10px;
    border-radius: 50%;
}

.dropdown-name {
    display: flex;
    flex-direction: column;
}

.dropdown-name h3 {
    color: #333;
}

.dropdown-role {
    margin-top: 10px;
    color: #666;
}

.dropdown-links {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}

.dropdown-links .navbar-link {
    margin-bottom: 0;
}

.dropdown-link {
    text-decoration: none;
    color: #007bff;
    margin-bottom: 5px;
}

.dropdown-link:hover {
    color: #0056b3;
}

.out {
    text-align: center;
}

.nav-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    border-radius: 5px;
    transition: background-color 0.3s ease;
}

.nav-item:hover {
    background-color: #f0f8ff;
}

.close-btn {
    margin-left: 10px;
    background-color: transparent;
    border: none;
    color: #666;
    cursor: pointer;
    transition: color 0.3s ease;
}

.close-btn:hover {
    color: #333;
}

.hidden {
    display: none;
}

.nav-item.active {
    background-color: #9d8d8d49;
    font-weight: bold;
}

.nav-item-left {
    display: flex;
    justify-content: flex-start;
    /* 从左边开始排列 */
    margin-left: 30px;
    /* 距离最左边的距离为30px */
}

.nav-item {
    /* 其他样式保持不变 */
    margin-right: 10px;
    /* 可以添加一些外边距，使导航项之间有间隔 */
}

.logout {
    text-align: center;
}
</style>
